<?php
/**
 * change sqlite3 to mysql  
 *
 * @filesource .php
 * @package 
 * @subpackage 
 * @version $id: 0.1, utf8, $
 * @author LD King <kldscs[at]gmail.com>
 * @copyright Copyleft (D.) 2007 - 2010 LD pr.
 * @link 
 * @example 
 *   
 */

$dbname = 'mifunny';
$dbfile = 'MyBlog.sqlite';

$tables = array('wp_commentmeta', 'wp_comments', 'wp_links', 'wp_options', 'wp_postmeta', 'wp_posts',
   'wp_terms', 'wp_term_relationships', 'wp_term_taxonomy', 'wp_usermeta', 'wp_users', );

$mysql = new PDO('mysql:dbname='.$dbname.';host=localhost;port=3306', 'root', 'shadow' );
$mysql->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); //setFetchMode fetch模式,关联数组
$mysql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sqlite = new PDO('sqlite:'.$dbfile);
$sqlite->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); //setFetchMode fetch模式,关联数组
$sqlite->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

foreach($tables as $tbl){
    $sql = "TRUNCATE TABLE {$tbl}";
    $mysql -> exec($sql);
    echo $sql."\t\n";

    $sql = "SELECT * FROM {$tbl}";
    $stmt = $sqlite->query($sql);
    $data = $stmt->fetchAll();
    foreach($data as $row){
        foreach($row as $k=>$v) 
            $row[$k] = $mysql->quote($v);

        $sql = "INSERT INTO {$tbl} VALUES ( ".implode(',', $row) ." ) ";
        $mysql -> exec($sql);
    }
}





